﻿namespace Service.Framework.Utils.Extension
{
    public static class DatetimeExtension
    {
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static String GetTimeStamp(this DateTime dateTime)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds).ToString();
        }

        /// <summary>
        /// 获取本月一号的零时零分零秒
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static DateTime GetMothFirstDay(this DateTime dateTime)
        {
            dateTime = dateTime.AddDays(-(dateTime.Day - 1));
            dateTime = dateTime.AddHours(-dateTime.Hour);
            dateTime = dateTime.AddMinutes(-dateTime.Minute);
            dateTime = dateTime.AddSeconds(-dateTime.Second);
            return dateTime;
        }

        /// <summary>
        /// 获取今天0点
        /// </summary>
        /// <param name="Now"></param>
        /// <returns></returns>
        public static DateTime GetZeroTime(this DateTime Now)
        {
            Now = Now.AddHours(-Now.Hour);
            Now = Now.AddMinutes(-Now.Minute);
            Now = Now.AddSeconds(-Now.Second);
            Now = Now.AddMilliseconds(-Now.Millisecond);
            return Now;
        }
    }
}
